package cn.buk.tms.hotel.service;

import cn.buk.common.util.HttpUtil;
import cn.buk.jedis.RedisDao;
import cn.buk.qms.HttpServletUtil;
import cn.buk.tms.hotel.dto.HotelCity;
import cn.buk.tms.hotel.dto.HotelSearchResult;
import cn.buk.tms.hotel.dto.RoomPriceDto;
import cn.buk.tms.hotel.sc.HotelSearchCriteria;
import cn.buk.common.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * @author yfdai
 */
@Component
public class HotelServiceImpl implements HotelService {

  @Value("${api_hotel_url}")
  private String apiUrl;

  @Autowired
  private RedisDao redisDao;

  @Override
  public HotelSearchResult searchHotel(HotelSearchCriteria sc) {

    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("sc.pageNo", sc.getPage().getPageNo() + ""));
    params.add(new BasicNameValuePair("sc.pageSize", sc.getPage().getPageSize() + ""));
    params.add(new BasicNameValuePair("sc.cityId", sc.getCityId() + ""));
    params.add(new BasicNameValuePair("sc.hotelId", sc.getHotelId() + ""));
    params.add(new BasicNameValuePair("sc.checkInDate", DateUtil.formatDate(sc.getCheckInDate(), "yyyy-MM-dd")));
    params.add(new BasicNameValuePair("sc.checkOutDate", DateUtil.formatDate(sc.getCheckOutDate(), "yyyy-MM-dd")));
    params.add(new BasicNameValuePair("sc.roomCount", sc.getRoomCount() + ""));
    params.add(new BasicNameValuePair("sc.guestCount", sc.getGuestCount() + ""));
    params.add(new BasicNameValuePair("sc.hotelStar", sc.getHotelStar() + ""));
    params.add(new BasicNameValuePair("sc.hotelName", sc.getHotelName()));
    params.add(new BasicNameValuePair("sc.district", sc.getDistrict()));
    params.add(new BasicNameValuePair("sc.businessDistrict", sc.getBusinessDistrict()));
    params.add(new BasicNameValuePair("sc.breakfast", sc.getBreakfast() + ""));

    params.add(new BasicNameValuePair("sc.hotelType", sc.getHotelType() + ""));

    final String url = apiUrl + "/avail2?";
    final String jsonStr = HttpUtil.postUrl(url, params);
      return JSON.parseObject(jsonStr, HotelSearchResult.class);
  }

  @Override
  public List<RoomPriceDto> searchRoomPrice(int roomId, Date checkInDate, Date checkOutDate) {
    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("roomId", roomId + ""));
    params.add(new BasicNameValuePair("checkInDate", DateUtil.formatDate(checkInDate, "yyyy-MM-dd")));
    params.add(new BasicNameValuePair("checkOutDate", DateUtil.formatDate(checkOutDate, "yyyy-MM-dd")));

    final String url = apiUrl + "/prices?";
    final String jsonStr = HttpUtil.postUrl(url, params);

    return JSON.parseArray(jsonStr, RoomPriceDto.class);
  }

  @Override
  public List<HotelCity> searchHotelCities() {
    final String url = apiUrl + "/cities?";

    final String key = "api_hotel_cities";
    final String temp = redisDao.getStr(key);
    if (temp != null) {
      return JSON.parseArray(temp, HotelCity.class);
    }

    final String jsonStr = HttpUtil.getUrl(url, null);
    List<HotelCity> rs = JSON.parseArray(jsonStr, HotelCity.class);

    //首次获取成功后，缓存到REDIS，超时30分钟
    if (!rs.isEmpty()) {
      redisDao.setAndExpire(key, jsonStr.getBytes(UTF_8), 60L * 30);
    }

    return rs;
  }
}
